//
//  HSYSelectProvinceViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/6.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYSelectProvinceViewController.h"
#import "UIImage+Tools.h"
#import "HSYGetCityRequest.h"
#import "HSYCityModel.h"
#import "BQActivityView.h"
#import "HSYSelectCityViewController.h"

@interface HSYSelectProvinceViewController ()

@property(nonatomic,copy)void(^selectedCityBlock)(NSString *title,NSString *code);

@property(nonatomic,strong)NSArray *dataList; //数据源

@end

@implementation HSYSelectProvinceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.tableView.tableFooterView = [UIView new];
    
    [self.leftAreaLBtn setImage:[UIImage imageNamed:@"left"]  forState:UIControlStateNormal];
    [self.titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.titleButton setTitle:@"选择省" forState:UIControlStateNormal];
    
    [self getList];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}

- (void)setupNavigationBarBackgroundImage{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(1, 1)]  forBarMetrics:UIBarMetricsDefault];
}

- (BOOL)hsy_allowPullUp{
    return NO;
}

- (BOOL)hsy_allowPullDown{
    return NO;
}

-(void)getProvinceCityCountyinfo:(void(^)(NSString *title,NSString *code))info{
    self.selectedCityBlock = info;
}

- (void)getList{
    
    HSYGetCityRequest *task = [[HSYGetCityRequest alloc] init];
    task.parentCode = @"0";
    
    __weak typeof(self)weakSelf = self;
    [BQActivityView showActiviTy];

    [HSYNetworkManager POST:task success:^(HSYResponse *response) {
        [BQActivityView hideActiviTy];

        if (response.resultCode == HSY_REQUEST_CODE_SUCCESS) {
            weakSelf.dataList = [HSYCityModel mj_objectArrayWithKeyValuesArray:response.encryptDataResult];
            [weakSelf.tableView reloadData];
        }else{
            [weakSelf showMessage:response.resultMessage];
        }
    } failure:^(NSError *error) {
        [BQActivityView hideActiviTy];

        if (error.code != NSURLErrorCancelled) {
            [weakSelf showMessage:@"请求超时"];
        }
    }];
}

#pragma mark UITableViewDelegate,UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    if (section == 1) {
//        return 10.0;
//    }
    return 0;
}

//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    
//    UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth(), 10.0)];
//    vi.backgroundColor = [UIColor clearColor];
//    
//    return vi;
//}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    if (section == 0) {
//        return 1;
//    }
    
    return self.dataList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    int i = 0;
    if (/*indexPath.section == 0*/1==i) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell1"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell1"];
        }
        cell.textLabel.text = @"当前城市";
        
        return cell;
        
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell2"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell2"];
        }
        
        HSYCityModel *model = self.dataList[indexPath.row];
        
        if (!model.isLeaf && (self.leafCode > 1 || self.leafCode == 0)) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }else{
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        
        cell.textLabel.text = model.aname;
        
        return cell;
    }
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HSYCityModel *model = self.dataList[indexPath.row];
    
    if (self.leafCode == 1) {
        self.selectedCityBlock(model.aname, model.code);
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        HSYSelectCityViewController *vc = [[HSYSelectCityViewController alloc] init];
        vc.leafCode = self.leafCode;
        __weak typeof(self)weakSelf = self;
        [vc getChildrenWithModel:model callback:^(NSString *title, NSString *code) {
            if (weakSelf.selectedCityBlock) {
                weakSelf.selectedCityBlock(title,code);
            }
        }];
        
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
